package com.elong.businesstravel.modules.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.android.support.jhf.edittextclear.EditTextClearView;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.android.support.jhf.utils.ConfigManager;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.activity.BaseActivity;
import com.elong.businesstravel.base.utils.AppUserId;
import com.elong.businesstravel.base.utils.SaveUserOperate;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.entitys.MemberLogin;
import com.elong.businesstravel.entitys.UnionLoginInfo;
import com.elong.businesstravel.modules.login.baseactivity.BaseLoginActivity;
import com.elong.businesstravel.modules.member.MemberCenterActivity;
import com.elong.businesstravel.protocol.login.MemberReccommendRequest;
import com.elong.businesstravel.protocol.login.MemberReccommendResponse;
import com.elong.businesstravel.protocol.login.MemberUnionCreateRequest;
import com.elong.businesstravel.protocol.login.MemberUnionCreateResponse;
import com.umeng.analytics.MobclickAgent;

/**
 * 注册成功填写邀请码界面,flag 0：手机号注册登陆跳转； 1：联合登陆跳转
 * 
 * @author lenovo
 * 
 */
public class RegisterSucceedActivity extends BaseLoginActivity {

	private ViewGroup unionLoginShowLayout;
	private EditTextClearView mInviteEditTextClearView;
	/** 0：手机号注册登陆跳转； 1：联合登陆跳转 */
	private int mFlag = 0;
	/** flag为1的时候起作用 */
	private MemberLogin mMemberLogin;
	private UnionLoginInfo mUnionLoginInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_succeed);
		init();
	}

	@Override
	protected void init() {
		mFlag = getIntent().getIntExtra("flag", 0);
		mMemberLogin = (MemberLogin) getIntent().getSerializableExtra(
				"MemberLogin");
		mUnionLoginInfo = (UnionLoginInfo) getIntent().getSerializableExtra(
				"UnionLoginInfo");
		super.init();

		ViewGroup inviteLayout = (ViewGroup) findViewById(R.id.inviteLayout);
		if (null != mMemberLogin && "0".equals(mMemberLogin.newuser)) {
			//老用户
			inviteLayout.setVisibility(View.GONE);
		} else {
			//新用户
			inviteLayout.setVisibility(View.VISIBLE);
		}
		unionLoginShowLayout = (ViewGroup) findViewById(R.id.unionLoginShowLayout);
		if (0 == mFlag) {
			unionLoginShowLayout.setVisibility(View.GONE);
		}

		mInviteEditTextClearView = (EditTextClearView) findViewById(R.id.inviteEditTextClearView);

		setListener();

		setTitleNavBar("注册成功", new OnClickListener() {

			@Override
			public void onClick(View v) {
				skipInvite();
			}
		});
	}

	@Override
	protected void setListener() {
		Button skipButton = (Button) findViewById(R.id.skipButton);
		if (unionLoginShowLayout.getVisibility() == View.VISIBLE) {
			skipButton.setVisibility(View.INVISIBLE);
		}
		skipButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				skipInvite();
			}
		});
		Button submitButton = (Button) findViewById(R.id.submitButton);
		submitButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (0 == mFlag) {
					if (TextUtils.isEmpty(mInviteEditTextClearView
							.getEditText().getText().toString())) {
						HandlerToastUI.getHandlerToastUI(mContext, "请填写邀请码");
					} else {
						memberReccommend();
					}
				} else if (1 == mFlag) {
					StringBuilder errorStringBuilder = new StringBuilder();
					if (null == mMemberLogin
							&& TextUtils.isEmpty(mInviteEditTextClearView
									.getEditText().getText().toString())) {
						HandlerToastUI.getHandlerToastUI(mContext, "请填写邀请码");
					} else if (!checkStartUse(errorStringBuilder)) {
						// error
						HandlerToastUI.getHandlerToastUI(mContext,
								errorStringBuilder.toString());
					} else {
						memberUnionCreate();

					}
				}
			}
		});

		super.setListener();
	}

	/**
	 * 发送邀请码
	 */
	private void memberReccommend() {
		NetworkClient.getNetworkClient().PostRequest(
				new MemberReccommendRequest(mContext,
						AppUserId.getUserId(mContext), mInviteEditTextClearView
								.getEditText().getText().toString()),
				new MemberReccommendResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						HandlerToastUI.getHandlerToastUI(mContext,
								messageString);
						submitSucceed();
					}
				});
	}

	/**
	 * 联合登陆 绑定手机号
	 */
	private void memberUnionCreate() {
		NetworkClient.getNetworkClient().PostRequest(
				new MemberUnionCreateRequest(mContext, mUnionLoginInfo.openid,
						mUnionLoginInfo.opentype, mUnionLoginInfo.screen_name,
						mPhoneEditTextClearView.getEditText().getText()
								.toString(), mYzmEditTextClearView
								.getEditText().getText().toString(),
						mInviteEditTextClearView.getEditText().getText()
								.toString()), new MemberUnionCreateResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						HandlerPostUI.getHandlerPostUI(new Runnable() {

							@Override
							public void run() {
								mMemberLogin = memberLogin;
								if ("0".equals(mMemberLogin.status)) {
									if (null != memberLogin) {
										AppUserId.setUserId(mContext,
												memberLogin.uid);
										submitSucceed();
									}
								} else {
									HandlerToastUI.getHandlerToastUI(mContext,
											mMemberLogin.msg);
								}

							}
						});
					}
				});
	}

	private void submitSucceed() {
		//联合登陆绑定手机号成功
		MobclickAgent.onEvent(mContext,"Union_LoginBinding_Phone");
		setResult(RESULT_OK);
		finish();
	}

	/**
	 * 跳过邀请码
	 */
	private void skipInvite() {
		if (unionLoginShowLayout.getVisibility() == View.VISIBLE) {
//			HandlerToastUI.getHandlerToastUI(mContext, "请关联手机号");
			setResult(RESULT_CANCELED);
			finish();
		} else {
			setResult(RESULT_OK);
			finish();
		}
	}

	// @Override
	// protected void setTitleNavBar() {
	// TitleNavBarView titleNavBarView = (TitleNavBarView)
	// findViewById(R.id.titleNavBarView);
	// titleNavBarView.setMessage("注册成功");
	// titleNavBarView.setLeftButtonVisibility(View.INVISIBLE);
	// titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	// }

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {

			skipInvite();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
